<?php
namespace App\Http\Controllers;
use App\Models\Food;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class  FoodController extends Controller{
    public function test1(){
        $food = DB::select('select * from onethink_food where id > ?',[10]);
        //$bool = DB::insert('insert into onethink_food(name,content,ctrlpos,inputtime,area,piclist,source,pubdate,address,price,type,foodstory) VALUES (?,?,?,?,?,?,?,?,?,?,?,?) ',
        //    ['haochi','123',2,1121212,2,1,1,12121,'常德',12,2,'']);
        //$num = DB::update('update onethink_food set name=? WHERE id =?',['海天蚝油',1]);
        dd($food);
        //$num = DB::delete('delete from student where id = ?',[1]);

    }
    public function insert(){
        $id = DB::table('food')->insertGetId(
            ['name'=>'imooc','content'=>'1123']
        );
        /*$bool = DB::table('food')->insert(
            ['name'=>'imooc','content'=>'1123']
        );*/
    }
    public function update(){
        /*$num = DB::table('food')
            ->where('id',1)
            ->update(
            ['name'=>'桂花糖1','type'=>'2']
        );*/
        $num = DB::table('food')
            ->where('id',1)
            ->increment('price',2,['name'=>'桂花糖1']);//自增
            //->decrement('price',2);自减
        var_dump($num);
    }
    public function delete(){
        $num = DB::table('food')
            ->where('id',20)
            ->delete();
        var_dump($num);
    }
    public function query(){
        //get()
        //$foods = DB::table('food')->where('id','>=',5)->get();
        //$foods = DB::table('food')->whereRaw('id >= ? and type = ?',[5,2])->get();

        //first()
        /*$food = DB::table('food')
            ->orderBy('id','desc')
            ->first();*/
        //pluck()
        //$names = DB::table('food')->pluck('name','id');
        //select
        //$students = DB::table('food')->select('id','name','content')->get();
        //dd($students);
        /*DB::table('food')->orderBy('id')->chunk(2,function ($students){
            echo '<pre>';
           var_dump($students);
            echo '</pre>';
            if (你的条件){
                return false;
            }
        });*/
        $num = DB::table('food')
            //->count();
            //->max('price');
            //->avg('price');
            ->sum('price');
        dd($num);
    }
    public function orm(){
        //$foods = Food::all();
        //$food = Food::find(2);
        //$food = Food::findOrFail(20);
        /*$foods = Food::where('id','>','3')
            ->orderBy('id','desc')
            //->get();
            ->first();
        */
        /*Food::chunk(2,function ($food){
            echo '<pre>';
            var_dump($food);
        });*/
        $num = Food::count();
        dd($num);
    }
    public function orm2(){
        $food = new Food();
        /*$food->name='sean';
        $food->content='123';
        $bool = $food->save();*/
        //$food = Food::find(1);
        //echo date('Y-m-d H:i:s',$food->inputtime);

        /*$food = Food::create(
            ['name'=>'酱板鸭','content'=>'123']
        );*/

        //firstOrCreate()==firstOrNew()->save()
        /*$food = Food::firstOrCreate(
            ['name'=>'桂花糖2']
        );*/
        //$num = Food::destroy([1]);
        dd($food);
    }
}